/***************************************************************************
 创建者：华磊
 开始时间：         : 2017.02.13
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   码垛指令视窗
*                                                                         *
 ***************************************************************************/
#include "palletinstructiondelegate.h"
#include <QDebug>
#include "hmicommondefine.h"
#include "instructionEnum.h"


PalletInstructionDelegate::PalletInstructionDelegate(GlobalData *globalDataIn, MoveOperation *moveOperationIn, QWidget *parent):
        QObject(parent)
{
    globalData = globalDataIn;

    moveOperation = moveOperationIn;
}

void PalletInstructionDelegate::initialView()
{
    palletInstructionView->palletHeadInstruction = "";
    palletInstructionView->palletTailInstruction = "";
    palletInstructionView->palletMotionInstruction = "";

   palletInstructionView->instructionTypeFlag = 0;
    palletInstructionView->palletPathTypeFlag = 0;
    palletInstructionView->approachPointIndex = 0;
    palletInstructionView->fleePointIndex = 0;

    palletInstructionView->instructionRate = 100;
    palletInstructionView->rateUnit = "%";
    palletInstructionView->rateUnitTypeFlag = E_RATE_PERCENT;
    palletInstructionView->fixedPositionTypeFlag = 0;
    palletInstructionView->cntValue = 100;
    palletInstructionView->currentIndex = 0;


    palletInstructionView->selectSkipFlag = 0;
    palletInstructionView->diIndex = "1";
    palletInstructionView->diSwitchFlag = 0;
    palletInstructionView->skipLabelIndex = "1";

    palletInstructionView->palletPointType = 0;

    palletInstructionView->palletPointIndex = "[A_1]";
    palletInstructionView->showPointNum = 1;
    palletInstructionView->changedApproachCount = 0;
    palletInstructionView->changedFleeCount = 0;


}

void PalletInstructionDelegate::selectJInstructionSlot()
{
    palletInstructionView->instructionTypeFlag = 0;
}

void PalletInstructionDelegate::selectLInstructionSlot()
{
    palletInstructionView->instructionTypeFlag = 1;
}

void PalletInstructionDelegate::recordPointSlot(int pointNum)
{
    qDebug()<<"PalletInstructionDelegate::recordPointSlot 机器人点";


}

void PalletInstructionDelegate::moveToPointSlot(int type, int pointNum)
{

}

void PalletInstructionDelegate::moveToPointFinishSlot()
{
    //emit stopMoveToPointSignal();
    moveOperation->stopPointMove();
}

void PalletInstructionDelegate::isSelectAuxiliarySlot()
{

}

void PalletInstructionDelegate::showPointInfoSlot(int pointNum)
{


}

void PalletInstructionDelegate::selectSkipInstructionSlot()
{
    if(0 == palletInstructionView->selectSkipFlag)
    {
        palletInstructionView->selectSkipFlag = 1;
    }
    else if(1 == palletInstructionView->selectSkipFlag)
    {
        palletInstructionView->selectSkipFlag = 0;
    }
}

void PalletInstructionDelegate::clearNoteSlot()
{

}

void PalletInstructionDelegate::finishHeadInstructionSlot()
{
    palletInstructionView->getInput();
    palletInstructionView->palletHeadInstruction = "PALLETIZING-";
    palletInstructionView->palletHeadInstruction += QString::number(palletInstructionView->palIns.stackNumber)
            + ";" + " //" + palletInstructionView->palIns.comment;
}

void PalletInstructionDelegate::finishMotionInstructionSlot()
{
    if(0 == palletInstructionView->instructionTypeFlag)
    {
        palletInstructionView->palletMotionInstruction = "J ";
    }
    else if(1 == palletInstructionView->instructionTypeFlag)
    {
        palletInstructionView->palletMotionInstruction = "L ";
    }

    palletInstructionView->palletMotionInstruction += "PAL_" + QString::number(palletInstructionView->palIns.stackNumber);

    if(EM_PALLET_PATH_APPROACH == palletInstructionView->palletPathTypeFlag)
    {
        palletInstructionView->palletMotionInstruction += "[A_" + QString::number(palletInstructionView->approachPointIndex) + "] ";
    }
    else if(EM_PALLET_PATH_FLEE == palletInstructionView->palletPathTypeFlag)
    {
        palletInstructionView->palletMotionInstruction += "[R_" + QString::number(palletInstructionView->fleePointIndex) + "] ";
    }
    else if(EM_PALLET_PATH_STACK == palletInstructionView->palletPathTypeFlag)
    {
        palletInstructionView->palletMotionInstruction += "[BTM] ";
    }

    palletInstructionView->palletMotionInstruction += QString("%1").arg(palletInstructionView->instructionRate)
            + palletInstructionView->rateUnit + " ";

    if(0 == palletInstructionView->fixedPositionTypeFlag)
    {
        palletInstructionView->palletMotionInstruction += "FINE";
    }
    else if(1 == palletInstructionView->fixedPositionTypeFlag)
    {
        palletInstructionView->palletMotionInstruction += "CNT" + QString::number(palletInstructionView->cntValue);
    }

    if(1 == palletInstructionView->selectSkipFlag)
    {
        palletInstructionView->palletMotionInstruction += " DI[" + palletInstructionView->diIndex + "]=";
        if(0 == palletInstructionView->diSwitchFlag)
        {
            palletInstructionView->palletMotionInstruction += "ON ";
        }
        else if(1 == palletInstructionView->diSwitchFlag)
        {
            palletInstructionView->palletMotionInstruction += "OFF ";
        }
        palletInstructionView->palletMotionInstruction += "Skip,LBL[" + palletInstructionView->skipLabelIndex + "]";
    }

    palletInstructionView->palletMotionInstruction = palletInstructionView->palletMotionInstruction
            + ";" + " //" + palletInstructionView->palIns.comment;
}

void PalletInstructionDelegate::changeLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/palletDelEnglishLan.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
}

